package com.tools.common.aop.permission;

import android.util.Log;

import com.tools.common.utils.PermissionManager;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class PermissionCheck {

    //@Pointcut("execution(@com.tools.common.aop.permission.RequirePermission * com.tools.mytools.serialization.SerActivity.*(..)) && @annotation(requirePermission)")
    @Pointcut("execution(@com.tools.common.aop.permission.RequirePermission * *..*.*(..)) && @annotation(requirePermission)")
    public void methodNeedsPermissions(RequirePermission requirePermission) {}

    @Around("methodNeedsPermissions(requirePermission)")
    public void checkPermission(final ProceedingJoinPoint joinPoint, final RequirePermission requirePermission) {
        PermissionManager.check(requirePermission.value(), new PermissionManager.Callback() {
            @Override
            public void onResult(boolean granted) {
            if(granted) {
                try {
                    Log.d("PermissionCheck", requirePermission.value() + " granted!");
                    joinPoint.proceed();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            } else {
                Log.d("PermissionCheck", requirePermission.value() + " not granted!");
            }
            }
        });
    }
}
